package com.itliang.web.controller.hardware;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itliang.annotation.Log;
import com.itliang.constant.MqttConstants;
import com.itliang.core.model.dto.equipment.EquipmentQueryRequest;
import com.itliang.core.model.entity.equipment.Equipment;
import com.itliang.core.model.vo.equipment.EquipmentVO;
import com.itliang.core.result.BaseResponse;
import com.itliang.core.result.CodeEnum;
import com.itliang.core.result.ResultUtils;
import com.itliang.enums.BusinessType;
import com.itliang.exception.BusinessException;
import com.itliang.mqtt.service.MqttService;
import com.itliang.service.hardware.EquipmentService;
import com.itliang.utils.mqtt.MqttMessageUtils;
import io.swagger.annotations.Api;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.itliang.constant.MqttConstants.MQTT_QOS_0;

/**
 * @author : liangzhuoyuan
 */
@Api(tags = "设备管理接口")
@RestController
@RequestMapping("/admin/hardware/equipment")
public class EquipmentController {

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private MqttService mqttService;

    @Log(title = "设备管理", businessType = BusinessType.STATUS)
    @GetMapping("/status")
    public BaseResponse<?> changeEquipmentStatus(Integer id, Integer status) throws MqttException {
        UpdateWrapper<Equipment> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id).set("status", status);
        equipmentService.update(wrapper);
        Equipment equipment = equipmentService.getById(id);
        ArrayList<Equipment> list = new ArrayList<>();
        list.add(equipment);
        String json = MqttMessageUtils.hardwareStatus(list);
        mqttService.publish(MqttConstants.SPRINGBOOT_TO_ESP_TOPIC, json, MQTT_QOS_0);
        return ResultUtils.success("恭喜，修改状态操作成功！");
    }

    // region 增删改查

    /**
     * 创建设备
     *
     * @param equipment
     * @return
     */
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public BaseResponse<Long> addEquipment(@RequestBody Equipment equipment) {
        equipmentService.save(equipment);
        return ResultUtils.success(equipment.getId(), "恭喜，添加操作成功！");
    }

    /**
     * 删除设备
     *
     * @param id 设备id
     * @return
     */
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    public BaseResponse<Boolean> deleteEquipment(int id) {
        boolean b = equipmentService.removeById(id);
        return ResultUtils.success(b, "恭喜，删除操作成功！");
    }

    /**
     * 批量删除设备
     *
     * @param idList id集合
     * @return
     */
    @Log(title = "设备管理", businessType = BusinessType.BATCHDELETE)
    @PostMapping("/delete/batch")
    public BaseResponse<Boolean> batchDeleteEquipment(@RequestBody List<Integer> idList) {
        boolean b = equipmentService.removeBatchByIds(idList);
        return ResultUtils.success(b, "恭喜，批量删除操作成功！");
    }

    /**
     * 更新设备
     *
     * @param equipment 设备信息
     * @return
     */
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public BaseResponse<Boolean> updateEquipment(@RequestBody Equipment equipment) throws MqttException {
        boolean result = equipmentService.updateById(equipment);
        Equipment byId = equipmentService.getById(equipment.getId());
        ArrayList<Equipment> list = new ArrayList<>();
        list.add(byId);
        String json = MqttMessageUtils.hardwareStatus(list);
        mqttService.publish(MqttConstants.SPRINGBOOT_TO_ESP_TOPIC, json, MQTT_QOS_0);
        return ResultUtils.success(result, "恭喜，修改操作成功！");
    }

    /**
     * 根据 id 获取设备
     *
     * @param id 设备id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Equipment> getEquipmentById(Integer id) {
        if (id <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        Equipment equipment = equipmentService.getById(id);
        return ResultUtils.success(equipment);
    }

    /**
     * 获取设备列表
     *
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<Equipment>> listEquipment() {
        List<Equipment> list = equipmentService.list();
        return ResultUtils.success(list);
    }


    /**
     * 分页获取设备数据列表
     *
     * @param equipmentQueryRequest 条件参数
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<EquipmentVO>> listEquipmentByPage(EquipmentQueryRequest equipmentQueryRequest) {
        long current = equipmentQueryRequest.getCurrent();
        long size = equipmentQueryRequest.getPageSize();
        Page<EquipmentVO> page = equipmentService.listEquipmentByPage(new Page<>(current, size), equipmentQueryRequest);
        return ResultUtils.success(page);
    }

    // endregion
}
